package Controller;

import Data.*;
import MyInterfaces.MassageType;
import Service.Main;
import Service.SendMassage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;

public class AddFriend {

    @FXML
    private ResourceBundle resources;

    private Socket socket;
    @FXML
    private Label userid;

    @FXML
    private URL location;

    @FXML
    private TextField getNumber;
    @FXML
    private Button ensure;

    @FXML
    private Button addfriend;

    @FXML
    private ImageView friendavatar;
    private String myid=null;

    @FXML
    private Text AddText;
    private String myemail=null;
    public void setMy(String myid,String myemail) {
        this.myid = myid;
        this.myemail=myemail;
    }


    @FXML
    void AddFriendAction(ActionEvent event) throws IOException {
        //确认发送请求
            if(myemail!=null)
            {
                event massage=new event();
                massage.setType(MassageType.Add_Friend);
                ChatData chatData=new ChatData(null,null,myid,userid.getText());
                massage.setT(chatData);

                new SendMassage().sendMassage(massage);
            }
            else
            {
                event massage=new event();
                massage.setType(MassageType.Add_Group);
                ChatMain chatMain= Main.AllContronller.getChatMainController();
                PrivateInformationData mydata=chatMain.getMydata();

                ChatData chatData=new ChatData(mydata.getAvatar(),null,mydata.getUserid(),getNumber.getText());
                massage.setT(chatData);

                new SendMassage().sendMassage(massage);
            }

    }

    public void ResultAddFriend(event massage)
    {
        if(massage.getType().equals(MassageType.Add_Friend_Success)||massage.getType().equals(MassageType.Add_Group_Success))
        {
            //添加成功
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("成功");
            alert.setHeaderText(null);
            alert.setContentText("发送请求成功");
            alert.showAndWait();
            Stage stage=(Stage)addfriend.getScene().getWindow();
            stage.close();
        }
        else
        {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("添加失败，有可能是你的网络不好哦!");
            alert.showAndWait();
        }
    }

    public void ReturnAddResult(event returnmassge)
    {
        if(returnmassge.getType().equals(MassageType.Return_Search_Friend_Fail))
        {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("查找失败，你可能输入了错误的号码，或者你找的人已经是你的好友!");
            alert.showAndWait();
        }
        else if(returnmassge.getType().equals(MassageType.Return_Search_Friend_Success))
        {
            PrivateInformationData frienddata=(PrivateInformationData) returnmassge.getT();
            Image image=new Image(new ByteArrayInputStream(frienddata.getAvatar()),150,150,true,true);
            Circle circle=new Circle(75,75,75);

            friendavatar.setVisible(true);
            friendavatar.setImage(image);
            friendavatar.setClip(circle);

            userid.setVisible(true);
            userid.setText(frienddata.getUserid());
            addfriend.setVisible(true);
        }
        else if(returnmassge.getType().equals(MassageType.Search_Group_Success))
        {
            GroupData groupData=(GroupData) returnmassge.getT();
            Image image;
            if(groupData.getGroup_avatar()==null)
            {
                image=new Image("file:"+"E:\\JDK\\QQClient\\nox\\src\\img\\2\\1.jpeg");
            }
            else image=new Image(new ByteArrayInputStream(groupData.getGroup_avatar()));

            Circle circle=new Circle(75,75,75);

            friendavatar.setVisible(true);
            friendavatar.setImage(image);
            friendavatar.setClip(circle);

            userid.setVisible(true);
            userid.setText(groupData.getGroup_id());
            addfriend.setVisible(true);
        }
        else if(returnmassge.getType().equals(MassageType.Search_Group_Fail))
        {
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setTitle("错误");
            alert.setHeaderText(null);
            alert.setContentText("查找失败，你可能输入了错误的号码，或者你找的人已经在你的群聊列表当中!");
            alert.showAndWait();
        }
    }

    @FXML
    void EnsureButton(ActionEvent event) throws IOException, ClassNotFoundException {
        if(myemail!=null)
        {
            //发送我要添加好友的消息
            if(myid.equals(getNumber.getText())||myemail.equals(getNumber.getText()))
            {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("错误");
                alert.setHeaderText(null);
                alert.setContentText("不可以输入自己的id或者邮箱!");
                alert.showAndWait();
                return;
            }
            //还需要发送自己的数据
            //    System.out.println("看看我执行了吗，为什么");
            event massage=new event();
            //这里需要重写，发送过去应该有这个人的头像、id和昵称
            massage.setType(MassageType.Search_Friend);
            ChatData chatData=new ChatData(null,null,myid,getNumber.getText());
            massage.setT(chatData);
            System.out.println(getNumber.getText());

            new SendMassage().sendMassage(massage);

        }
        else
        {
            //请求加入群聊,发送自己和群聊的id，用于请求
            event massage=new event();
            massage.setType(MassageType.Search_Group);
            MassageData massageData=new MassageData();
            massageData.setSender(myid);
            massageData.setReceiver(getNumber.getText());
            massage.setT(massageData);

            new SendMassage().sendMassage(massage);
        }

    }

    @FXML
    void initialize()
    {
        assert ensure != null : "fx:id=\"ensure\" was not injected: check your FXML file 'Controller.AddFriend.fxml'.";
        assert addfriend != null : "fx:id=\"addfriend\" was not injected: check your FXML file 'Controller.AddFriend.fxml'.";
        addfriend.setVisible(false);
        userid.setVisible(false);
        this.socket=loginController.getSocket();
    }

    public void setAddGroup() {
        AddText.setText("加入群聊");
        addfriend.setText("确认加入");
    }
}
